//
//  MainBodyViewController.m
//  ERP
//
//  Created by goldeney on 15/1/12.
//  Copyright (c) 2015年 ShangHai Goldeneye Techonology Co.,Ltd. All rights reserved.
//

#import "MainBodyViewController.h"
#import "GetUserBasicInfoService.h"
#import "QueryMessageService.h"
#import "GetUserStationService.h"
#import "BasicConfigurationService.h"

#import "ExitLoginService.h"
#import "LocalNotification.h"
#import "PlaySoundUtil.h"
#import "NSKeyedArchiverUserLogin.h"
#import "NSDictionary+DataFormat.h"
#import <SDWebImage/UIImageView+WebCache.h>

#import "MainUserCenterViewController.h"
#import "MainDataCenterViewController.h"
#import "MainHomePageViewController.h"
#import "MessageCenterViewController.h"
#import "MainIndustrialControlController.h"
#import "ScanCodeViewController.h"
#import "WebMainViewController.h"
#import "EnterpriseViewController.h"
#import "EnterpriseWebController.h"
#import "WebTabBarController.h"
#import "MainHomePageNewController.h"

#import "TXSoundPlayer.h"
#import "UINavigationBar+Awesome.h"
#import "SYNavigationDropdownMenu.h"
#import "UIImage+Color.h"

#import "MessageInfoService.h"
#import "OfficeAutomationController.h"
#import "MessageInfoEntity.h"
#import "RTSpinKitView.h"
#import "UIView+AssistView.h"

@interface MainBodyViewController ()<UITabBarControllerDelegate>

@property (strong, nonatomic) UILabel *navSubLabel;
@property (assign, nonatomic) NSInteger messageCount;

@property (nonatomic,assign) NSInteger sessionUnreadCount;
@property (nonatomic,assign) NSInteger systemUnreadCount;
@property (nonatomic,assign) NSInteger customSystemUnreadCount;

@property (nonatomic,strong) SYNavigationDropdownMenu *menuDataCenter;

@end

@implementation MainBodyViewController

- (id)init
{
    self = [super init];
    if (self) {
        //添加菜单权限获取注册
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMenuAuthorityService:)
                                                     name:GetUserBasicInfoServiceKey object:nil];
        //添加消息获取注册
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMenuAuthorityService:)
                                                     name:MessageInfoServiceKey object:nil];
    }
    return self;
}
    
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //获取消息
    [MessageInfoService getMessageInfoSaveToSqlite];
}

- (void)dealloc
{
    //移除菜单权限注册
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark -
#pragma mark - NSNotificationCenter

-(void)updateMenuAuthorityService:(NSNotification*)notification
{
    //获取菜单权限表通知
    if ([[notification name] isEqualToString:GetUserBasicInfoServiceKey]){
        if(notification.object){
            [ExitLoginService exitAppUserLogin];//退出登录
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:notification.object delegate:nil
                                                cancelButtonTitle:@"确定" otherButtonTitles:nil];
            alert.alertViewStyle=UIAlertViewStyleDefault;
            [alert show];
        } else {
            [self drawTabBarTool:YES];// 刷新TabBar菜单
        }
    }
    
    //收到新消息
    else if ([[notification name] isEqualToString:MessageInfoServiceKey]){
        NSInteger markNumber = 0;
        NSArray *valueArray = [MessageInfoEntity MR_findAll];
        for(MessageInfoEntity *entity in valueArray)
            markNumber += entity.readCount.integerValue;
        _messageCount = markNumber;
        
        UIViewController *viewController = [self selectedViewController];
        if([viewController isKindOfClass:[MainUserCenterViewController class]])
            [self setViewControllerWhiteBadgeNumber:_messageCount];
        else
            [self setViewControllerBadgeNumber:_messageCount];
    }
}

#pragma mark -
#pragma mark - 页面处理

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.hidesBackButton = YES;
    self.delegate = self;
    self.tabBar.tintColor = ThemeColorBlue;
    self.tabBar.backgroundImage=[UIImage imageNamed:@"tabBarbg"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view showViewProgressRect:self.view.frame];
    
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:10]} forState:UIControlStateNormal];
    
    //添加TabBar
    [self drawTabBarTool:NO];
    
    //消息中心按钮
    [self setViewControllerBadgeNumber:0];
    
    //获取用户消息
    [[QueryMessageService sharedService] startQueryMessage];
    
    //获取用户基础数据
    [[GetUserBasicInfoService sharedService] startQueryService];
    
    //获取站点信息
    [[GetUserStationService sharedService] startQueryService];
    
    //获取基础配置信息
    [[BasicConfigurationService sharedService] startQueryService];
}

#pragma mark - TabBar变更
- (void)drawTabBarTool:(BOOL)isAllowExit {
    //移除所有ViewControllers
    NSArray *arrayView = self.childViewControllers;
    for(UIViewController *view in arrayView){
        [view removeFromParentViewController];
    }
    
    //获取主模块
    NSPredicate *predicateMain = [NSPredicate predicateWithFormat:@"parentId = %@", @0];
    NSArray *array = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:predicateMain];
    
    NSMutableArray *viewControllers = [NSMutableArray array];
    for (int i=0; i<array.count; i++){
        ModuleAuthorityEntity *entity = [array objectAtIndex:i];
        NSString *title = entity.title;
        NSString *descripter = (entity.descripter.length <= 0) ? entity.title : entity.descripter;
        
        if (entity.moduleType.integerValue == 1 && entity.codeType.integerValue == 1) {
            MainHomePageViewController *homeView = [[MainHomePageViewController alloc]init];
            homeView.tabBarItem.title= title;
            homeView.tagObject = descripter;
            homeView.tabBarItem.image = [[UIImage imageNamed:@"bar_home_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            homeView.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_home_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:homeView];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:homeView];
            
        } else if (entity.moduleType.integerValue == 8 && entity.codeType.integerValue == 1) {//新首页
            MainHomePageNewController *homeView = [[MainHomePageNewController alloc]init];
            homeView.tabBarItem.title= title;
            homeView.tagObject = descripter;
            homeView.tabBarItem.image = [[UIImage imageNamed:@"bar_home_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            homeView.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_home_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:homeView];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:homeView];
            
        } else if(entity.moduleType.integerValue == 2 && entity.codeType.integerValue == 1){
            MainDataCenterViewController *messageView = [[MainDataCenterViewController alloc] init];
            messageView.tabBarItem.title = title;
            messageView.tagObject = descripter;
            messageView.tabBarItem.image=[[UIImage imageNamed:@"tabbar_job_mangae_nor_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            messageView.tabBarItem.selectedImage = [[UIImage imageNamed:@"tabbar_job_mangae_pre_icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:messageView];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:messageView];
            
        } else if(entity.moduleType.integerValue == 3 && entity.codeType.integerValue == 1){
            MainIndustrialControlController *addressBook = [[MainIndustrialControlController alloc] init];
            addressBook.tabBarItem.title = title;
            addressBook.tagObject = descripter;
            addressBook.tabBarItem.image= [[UIImage imageNamed:@"bar_ad_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            addressBook.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_ad_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:addressBook];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:addressBook];
            
        } else if(entity.moduleType.integerValue == 4 && entity.codeType.integerValue == 1){
            MainUserCenterViewController *userCenter = [[MainUserCenterViewController alloc] init];
            userCenter.tabBarItem.title = title;
            userCenter.tagObject = descripter;
            userCenter.tabBarItem.image= [[UIImage imageNamed:@"bar_user_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            userCenter.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_user_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:userCenter];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:userCenter];
            
        } else if(entity.moduleType.integerValue == 6 && entity.codeType.integerValue == 1){
            EnterpriseViewController *enterpriseView = [[EnterpriseViewController alloc] init];
            enterpriseView.tabBarItem.title = title;
            enterpriseView.tagObject = descripter;
            enterpriseView.tabBarItem.image=[[UIImage imageNamed:@"bar_kb_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            enterpriseView.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_kb_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:enterpriseView];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:enterpriseView];
            
        } else if(entity.moduleType.integerValue == 7 && entity.codeType.integerValue == 1){//新版企业看板
            EnterpriseWebController *enterpriseView = [[EnterpriseWebController alloc] init];
            enterpriseView.tabBarItem.title = title;
            enterpriseView.tagObject = descripter;
            enterpriseView.tabBarItem.image=[[UIImage imageNamed:@"bar_kb_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            enterpriseView.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_kb_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [viewControllers addObject:enterpriseView];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:enterpriseView];
            
        }
        
        //网页通用模板
        else if(entity.moduleType.integerValue == 0 && entity.codeType.integerValue == 0){
            //网页模式参数
            WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
            model.URL = entity.webCode;
            model.CodeType = CodeTypeWeb;
            
            WebTabBarController *webView = [[WebTabBarController alloc] initWithPushParameterModel:model];
            webView.tabBarItem.title = title;
            webView.tagObject = descripter;
            webView.tabBarItem.image = [[UIImage imageNamed:@"bar_home_nor"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            webView.tabBarItem.selectedImage = [[UIImage imageNamed:@"bar_home_pre"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            webView.isNavigationBarDefault = YES;
            webView.isShowProgress = YES;
            webView.isHiddenNavigationProgress = YES;
            [viewControllers addObject:webView];
            
            //使用SDWebImage来下载及缓存
            NSArray *image = [entity.icon componentsSeparatedByString:@","];
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:image.firstObject] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                if (!error && finished) {
                    UIImage *newImage = [self reSizeImage:image toSize:CGSizeMake(32, 32)];
                    webView.tabBarItem.image = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                }
            }];
            
            [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:image.lastObject] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                if (!error && finished) {
                    UIImage *newImage = [self reSizeImage:image toSize:CGSizeMake(32, 32)];
                    webView.tabBarItem.selectedImage = [newImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
                }
            }];
            //默认主页
            if(i == 0) [self tabBarController:self didSelectViewController:webView];
        }
    }
    
    //设置显示ViewControllers
    self.viewControllers = viewControllers;
    self.moreNavigationController.navigationBarHidden= YES;
    
    // 若大模块一个都没有分配，退出登录
    if(isAllowExit && viewControllers.count <= 0) {
        [ExitLoginService exitAppUserLogin];//退出登录
        NSString *message = @"您未分配模块使用权限，请联系相关负责人开通";
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:self
                                            cancelButtonTitle:@"确定" otherButtonTitles:nil];
        alert.alertViewStyle=UIAlertViewStyleDefault;
        [alert show];
    }
    
    //移除进度条
    if (viewControllers.count > 0) {
        [self.view removeProgressFromSuperview];
    }
    
    //默认选择第一个
    if(array.count > 0) {
        [self setSelectedIndex:0];
    }
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //设置标题栏
    [self setStationTitleInfoTabBar:tabBarController viewController:viewController];
    
    if([viewController isKindOfClass:[MainUserCenterViewController class]]){
        [self setViewControllerWhiteBadgeNumber:_messageCount];
        //白色
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                           [UIColor whiteColor], NSForegroundColorAttributeName, [UIFont boldSystemFontOfSize:18.0],
                                                                         NSFontAttributeName, nil]];
    } else {
        [self setViewControllerBadgeNumber:_messageCount];
        //黑色
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                           [UIColorDispose colorWithHexString:@"#333333"], NSForegroundColorAttributeName, [UIFont systemFontOfSize:18.0], NSFontAttributeName, nil]];
    }
}

//黑色文字和按钮
- (void)setViewControllerBadgeNumber:(NSInteger)count
{
    UIButton *rightButton2 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
    [rightButton2 setImage:[UIImage imageNamed:@"home_contacts"] forState:UIControlStateNormal];
    [rightButton2 setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    UIBarButtonItem *rightItem2 = [[UIBarButtonItem alloc] initWithCustomView:rightButton2];
    [rightButton2 addTarget:self action:@selector(addressBookEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton4 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
    [rightButton4 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -15)];
    UIBarButtonItem *rightItem4 = [[UIBarButtonItem alloc] initWithCustomView:rightButton4];
    [rightButton4 addTarget:self action:@selector(messageInfoEvent:) forControlEvents:UIControlEventTouchUpInside];
    if(count <= 0)[rightButton4 setImage:[UIImage imageNamed:@"message_center_icon_no"] forState:UIControlStateNormal];
    else [rightButton4 setImage:[UIImage imageNamed:@"message_center_icon"] forState:UIControlStateNormal];
    
//    UIButton *rightButton3 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
//    [rightButton3 setImage:[UIImage imageNamed:@"home_scan"] forState:UIControlStateNormal];
//    [rightButton3 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -15)];
//    UIBarButtonItem *rightItem3 = [[UIBarButtonItem alloc] initWithCustomView:rightButton3];
//    [rightButton3 addTarget:self action:@selector(scanInfoEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItems = @[rightItem4];
    self.navigationItem.leftBarButtonItems = @[rightItem2];
}

//白色文字和按钮
- (void)setViewControllerWhiteBadgeNumber:(NSInteger)count
{
    UIButton *rightButton2 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
    [rightButton2 setImage:[UIImage imageNamed:@"home_contacts_w"] forState:UIControlStateNormal];
    [rightButton2 setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
    UIBarButtonItem *rightItem2 = [[UIBarButtonItem alloc] initWithCustomView:rightButton2];
    [rightButton2 addTarget:self action:@selector(addressBookEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *rightButton4 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
    [rightButton4 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -15)];
    UIBarButtonItem *rightItem4 = [[UIBarButtonItem alloc] initWithCustomView:rightButton4];
    [rightButton4 addTarget:self action:@selector(messageInfoEvent:) forControlEvents:UIControlEventTouchUpInside];
    if(count <= 0)[rightButton4 setImage:[UIImage imageNamed:@"message_center_icon_no_w"] forState:UIControlStateNormal];
    else [rightButton4 setImage:[UIImage imageNamed:@"message_center_icon_w"] forState:UIControlStateNormal];
    
//    UIButton *rightButton3 = [[UIButton alloc] initWithFrame:CGRectMake(0,0,32,40)];
//    [rightButton3 setImage:[UIImage imageNamed:@"home_scan_w"] forState:UIControlStateNormal];
//    [rightButton3 setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -15)];
//    UIBarButtonItem *rightItem3 = [[UIBarButtonItem alloc] initWithCustomView:rightButton3];
//    [rightButton3 addTarget:self action:@selector(scanInfoEvent:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItems = @[rightItem4];
    self.navigationItem.leftBarButtonItems = @[rightItem2];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //去除黑色阴影线
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6){
        [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    }
    
    if(self.menuDataCenter)
    [self.menuDataCenter hide];
}

#pragma mark === 永久闪烁的动画 ======

-(CABasicAnimation *)opacityForever_Animation:(float)time
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"opacity"];//必须写opacity才行。
    animation.fromValue = [NSNumber numberWithFloat:1.0f];
    animation.toValue = [NSNumber numberWithFloat:0.1f];//这是透明度。
    animation.autoreverses = YES;
    animation.duration = time;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];///没有的话是均匀的动画。
    return animation;
}

#pragma mark 设置标题栏

- (void)setStationTitleInfoTabBar:(UITabBarController *)tabBarController viewController:(UIViewController *)viewController
{
    self.navigationItem.titleView = nil;
    
    //判断是否是其他viewController
    if(![viewController isKindOfClass:[AbstractBaseViewController class]]) {
        [self setTitle:@"更多"]; return;
    }
    
    AbstractBaseViewController *baseController = (AbstractBaseViewController*)viewController;
    NSString *title = baseController.tagObject;
    if([viewController isKindOfClass:[MainHomePageViewController class]]){
        //业务管理，主页
        [self setTitle:title];
    } else if([viewController isKindOfClass:[MainHomePageNewController class]]){
        //业务管理，主页（新版本）
        [self setTitle:title];
    } else if ([viewController isKindOfClass:[EnterpriseViewController class]]) {
        //企业看板
        [self setTitle:title];
    } else if ([viewController isKindOfClass:[EnterpriseWebController class]]) {
        //企业看板WEB
        [self setTitle:title];
    } else if ([viewController isKindOfClass:[WebMainViewController class]]) {
        //生产汇总
        [self setTitle:title];
    } else if ([viewController isKindOfClass:[MainDataCenterViewController class]]) {
        //"数据统计
        self.menuDataCenter = [[SYNavigationDropdownMenu alloc] initWithNavigationController:
                                          self.navigationController];
        _menuDataCenter.titleLabel.font = [UIFont systemFontOfSize:18];
        [_menuDataCenter setTitle:title forState:UIControlStateNormal];
        [_menuDataCenter setImage:[UIImage imageNamed:@"Arrow"] forState:UIControlStateNormal];
        [_menuDataCenter setTitleColor:[UIColorDispose colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        self.navigationItem.titleView = _menuDataCenter;
        
    } else if ([viewController isKindOfClass:[MainIndustrialControlController class]]) {
        //智能硬件
        [self setTitle:title];
    } else if ([viewController isKindOfClass:[MainUserCenterViewController class]]) {
        //个人中心
        [self setTitle:title];
    }
    
    //网页通用模块
    else if ([viewController isKindOfClass:[WebTabBarController class]]) {
        [self setTitle:title];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [ExitLoginService exitAppUserLogin];//退出登录
}

#pragma mark Button Event 通讯录

- (void)addressBookEvent:(id)sender
{
    OfficeAutomationController *viewController = [[OfficeAutomationController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)messageInfoEvent:(id)sender
{
    MessageCenterViewController *viewController = [[MessageCenterViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}

- (void)scanInfoEvent:(id)sender
{
    ScanCodeViewController *viewController = [[ScanCodeViewController alloc] init];
    [self.navigationController pushViewController:viewController animated:YES];
}


#pragma mark Other

- (UIImage *)reSizeImage:(UIImage *)image toSize:(CGSize)reSize
{
    //UIGraphicsBeginImageContext(CGSizeMake(reSize.width, reSize.height));
    UIGraphicsBeginImageContextWithOptions(reSize, NO, 2);
    [image drawInRect:CGRectMake(0, 0, reSize.width, reSize.height)];
    UIImage *reSizeImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return reSizeImage;
}

@end
